package cn.wolfcode.wolf2w.strategy.controller;

import cn.wolfcode.wolf2w.common.core.context.SecurityContextHolder;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.security.annotation.InnerAuth;
import cn.wolfcode.wolf2w.common.security.annotation.RequiresLogin;
import cn.wolfcode.wolf2w.strategy.ann.CurrentUser;
import cn.wolfcode.wolf2w.strategy.entity.Strategy;
import cn.wolfcode.wolf2w.strategy.entity.StrategyCatalog;
import cn.wolfcode.wolf2w.strategy.entity.StrategyContent;
import cn.wolfcode.wolf2w.strategy.query.StrategyQuery;
import cn.wolfcode.wolf2w.strategy.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.strategy.service.IStrategyService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 攻略 Controller
 * 
 * @author libai
 * @date 2024-07-24
 */
@RestController
@RequestMapping("/strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    /**
     * 攻略详情
     */
    @GetMapping("/detail/{id}")
    public R<Strategy> detail(@PathVariable Long id) {
        Strategy strategy = strategyService.queryDetailById(id);
        return R.ok(strategy);
    }
    /**
     * 攻略 列表
     */
    @GetMapping("/query")
    public R<IPage<Strategy>> query(StrategyQuery qo) {
        IPage<Strategy> page = strategyService.queryPage(qo);
        return R.ok(page);
    }

    /**
     * 点击量前三
     * @param destId
     * @return
     */
    @GetMapping("/viewnnumTop3")
    public R<List<Strategy>> viewnymTop3(Long destId){
        List<Strategy> list = strategyService.queryViewTop3ByDestId(destId);
        return R.ok(list);
    }

    /**
     *   按目的地查询攻略分类
     * @param destId
     * @return
     */
    @GetMapping("/catalogList")
    public R<List<StrategyCatalog>> catalogList(Long destId){
        List<StrategyCatalog> list = strategyCatalogService.queryCatalogListByDestId(destId);
        return R.ok(list);
    }

    /**
     * 根据具体的 攻略ID查询 攻略文章内容
     * @param id
     * @return
     */
    @GetMapping("/content")
    public R<StrategyContent> content(Long id){
       StrategyContent content= strategyService.getContent(id);
       return R.ok(content);
    }

    /**
     *  浏览数 +1
     * @param sid
     * @return
     */
    @PostMapping("/viewnumIncr")
    public R<Map<String,Object>> viewnumIncr(Long sid){
        Map<String,Object> map = strategyService.viewnumIncr(sid);
        return R.ok(map);
    }

    //redis 评论数 +1
    @PostMapping("/replynumIncr")
    public R<Map<String,Object>> replynumIncr(Long sid){
        Map<String,Object> map = strategyService.incrCommentCount(sid);
        return R.ok(map);
    }



    @RequiresLogin
    @PostMapping("/favor")
    public R<Map<String,Object>> favor(Long sid , @CurrentUser Long uid ){
//        Long uid = SecurityContextHolder.getUserId();
        Map<String,Object> map = strategyService.favor(sid,uid);
        return R.ok(map);
    }

    @GetMapping("/isUserFavor")
    public R<Boolean> isUserFavor(Long sid, Long uid){
        boolean b = strategyService.isUserFavor(sid,uid);
        return R.ok(b);
    }

    /**
     * 点赞
     */
    @PostMapping("/thumbsup")
    public R<Map<String,Object>> praise(Long sid , @CurrentUser Long uid ){
//        Long uid = SecurityContextHolder.getUserId();
        Map<String,Object> map = strategyService.thumbsup(sid,uid);
        return R.ok(map);
    }




    /*****************************************对外暴露Fegin接口**********************************************/
    /**
     * Feign 接口
     */
    @GetMapping("/feign/list")
    public R<List<Strategy>> feignList() {
        return R.ok(strategyService.list());
    }
    @InnerAuth
    @GetMapping("/feign/{id}")
    public R<Strategy> feignGet(@PathVariable Long id) {
        return R.ok(strategyService.getById(id));
    }




    @PostMapping("/feign/statisHashJob")
    public void statisHashJob(){
        strategyService.statisHashDataPersistence();
    }
}
